package org.netEase.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.netEase.dao.zhutizuDao;
import org.netEase.entity.shangping;
import org.netEase.entity.zhuti;
import org.netEase.entity.zhutizu;
import org.netEase.util.BaseDao;

public class zhutizuDaoImpl extends BaseDao implements zhutizuDao {

	@Override
	public List<zhutizu> getbyYid(int Yid) {
		List<zhutizu> list = new ArrayList<>();
		String sql = "select * from zhutizu where Yid = ?";
		ResultSet rs = super.executeQuery(sql,Yid);
		try {
			while (rs.next()) {
				zhutizu zhutizu = new zhutizu();
				zhutizu.setYid(rs.getInt("yid"));
				zhutizu.setZid(rs.getInt("zid"));
				zhutizu.setzName(rs.getString("zName"));
				zhutizu.setZimg(rs.getString("zimg"));
				
				list.add(zhutizu);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	
	public static void main(String[] args) {
		zhutizuDao zhutizuDao = new zhutizuDaoImpl();
		List<zhutizu> list=zhutizuDao.getbyYid(1);
		for (zhutizu zhutizu : list) {
			System.out.println(zhutizu);
		}
	}

	@Override
	public List<zhutizu> getbyYids() {
		List<zhutizu> list = new ArrayList<>();
		String sql = "select * from zhutizu";
		ResultSet rs = super.executeQuery(sql);
		try {
			while (rs.next()) {
				zhutizu zhutizu = new zhutizu();
				zhutizu.setYid(rs.getInt("yid"));
				zhutizu.setZid(rs.getInt("zid"));
				zhutizu.setzName(rs.getString("zName"));
				zhutizu.setZimg(rs.getString("zimg"));
				
				list.add(zhutizu);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
//	public static void main(String[] args) {
//		zhutizuDao zu= new zhutizuDaoImpl();
//		List<zhutizu> list=zu.getbyYids();
//		for (zhutizu zhutizu : list) {
//			System.out.println(zhutizu);
//	}
//		
//	}


	@Override
	public List<zhutizu> getID(int Yid) {
		List<zhutizu> list = new ArrayList<>();
		List<Object> condition = new ArrayList<>();
		String sql = "SELECT * FROM `zhutizu` AS a INNER JOIN zhuti AS b ON a.`Yid`=b.`Yid` WHERE 1=1 ";
		if ( Yid != -1) {
			sql += " AND a.`Yid`=?";
			condition.add(Yid);
		}
		ResultSet rs = super.executeQuery(sql,condition.toArray());
		try {
			while(rs.next()) {
				zhutizu zu=new zhutizu();
				zu.setZid(rs.getInt("zid"));
				zu.setYid(rs.getInt("yid"));
				zu.setzName(rs.getString("zName"));
				zu.setZimg(rs.getString("zimg"));
				zhuti zhu= new zhuti();
				zhu.setYid(rs.getInt("yid"));
				zhu.setName(rs.getString("name"));
				zhu.setZtImages(rs.getString("ztImages"));
				zu.setZhuti(zhu);
				list.add(zu);
				
			}
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				super.closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}


	@Override
	public int add(zhutizu z) {
		  String sql="INSERT  INTO zhutizu(yid,zname,zimg)VALUES(?,?,?)";
		   return super.executeUpdate(sql, z.getYid(),z.getzName(),z.getZimg());
	}


	@Override
	public int dele(int Zid) {
		String sql = "delete from zhutizu where zid = ?";
		return super.executeUpdate(sql, Zid);
	}

}
